(function($){
	$.fn.jqpicbox = function(options) {
	
		//Assign default values. These can be over-written by defined options from the calling function.
		var defaults = {
			screenbgcolor: '#003D1F',
			picborder: '2px solid #FFFFFF',
			picbgcolor: '#FFFFFF',
			piccolor: '#FFFFFF',
			picheight: '400',
			linkborder: '1px solid #FF6633'
		},
		
		//Here we merge the options & defaults into the "settings" variable.
		settings = $.extend({}, defaults, options);
		
		//Iterate through elements passed to jqpicbox and make sure that they are links.
		//If they are valid links, attach the onClick function.
		return this.each(function() {
			if($(this).is('a') && $(this).attr('href') != ''){
				var href = $(this).attr('href');
				$(this).click(picBox);
				$(this).blur(function () {
					$(this).attr('href',href);
				});
			}
		});
		
		//Definition of the onClick function attached to selected links.
		//Basically we're just appending a "screen" and "dialogue" box to the page and presenting the defined variables and confirmation links.
		function picBox() {
			
			$(this).focus();
			//assign some important variables. Required for positioning and presenting the confirmed link.
			var href = $(this).attr('href'),
			divLeft = document.documentElement.clientWidth/2 - settings.picheight/2,
			divTop = document.documentElement.clientHeight/2 - settings.picheight/2;
			//Remove the link's href attribute.
			$(this).removeAttr('href');
			//Append-Prepend the screen & dialogue elements with styles.
			$('body').append('<div id="jqpicbox_screen"></div>');
			$('#jqpicbox_screen').hide();
			$('#jqpicbox_screen').css({
				backgroundColor: settings.screenbgcolor
				});
			$('body').append('<div id="jqpicbox_message"></div>');
			$('#jqpicbox_message').css({
				color: settings.piccolor,
				border: settings.picborder,
				backgroundColor: settings.picbgcolor,
				height: settings.picheight+'px',
				left: divLeft+'px',
				bottom: divTop+'px'
				});
			$('#jqpicbox_message').hide();
			$('#jqpicbox_message').prepend('<img id="jqpicbox_photo_container" height="'+(settings.picheight-20)+'" />');
			$('#jqpicbox_photo_container').load(href);
			$('#jqpicbox_photo_container').attr('src',href);
			$('#jqpicbox_screen').click(function () {
				$('#jqpicbox_message').remove();
				$('#jqpicbox_screen').remove();
			});
			//Finally, show the appended and prepended elements.
			$('#jqpicbox_screen').show();
			$('#jqpicbox_message').slideDown('slow',closeButton);
		}
		
		function closeButton() {
			$('#jqpicbox_message').prepend('<div id="jqpicbox_close_link" class="jqpicbox_hide"><a href="Javascript: void(0)">close</a></div>');
			//Attach an onClick to the "Cancel" link that hides the elements created above.
			$('.jqpicbox_hide').click(function () {
				$('#jqpicbox_message').remove();
				$('#jqpicbox_screen').remove();
			});
		}
	}
})(jQuery);
